import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex)

export default new Vuex.Store({
    state: {
        todolist: [
            { id: 100, name: "吃饭", isDone: true },
            { id: 101, name: "睡觉", isDone: false },
            { id: 102, name: "打豆豆", isDone: true },
        ],
        isSell: 'all',
        // showArr:[]
    },
    mutations: {
        isSell(state, payload) {
            state.isSell = payload
        },
        addtodolist(state, payload) { 
          state.todolist.push(payload);
        },
        deltodolist(state, id) {
            let index = state.todolist.findIndex(obj => obj.id === id)
            state.todolist.splice(index,1)
        },
        proform(state, payload) {
            state.isSell = payload
            // state.showArr =state.todolist.map(obj =>  obj)
            // console.log(state.showArr);
        },
        noproform(state, payload) { 
            state.isSell = payload
            // state.showArr = state.todolist.filter(obj => {
            //    return obj.isDone === false
            // })
            // console.log(state.showArr);
        },
        yesproform(state, payload) { 
            state.isSell = payload
            // state.showArr = state.todolist.filter(obj => {
            //    return obj.isDone === true
            // })
        },
        clearAll(state) {
           state.todolist = state.todolist.filter(obj => obj.isDone === false)
        }
    },
    actions: {},
    getters: {
        showArr:(state)=>{
            if (state.isSell === 'yes') { // 显示已完成
                return state.todolist.filter(obj => {
                    return obj.isDone === true
                })
            } else if (state.isSell === 'no') { // 显示未完成
              return state.todolist.filter(obj => obj.isDone === false)
            } else {
              return state.todolist // 全部显示
            }
        }
    },

})